package cn.iocoder.yudao.module.wine.controller.admin.supplyorder;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wine.controller.admin.supplyorder.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplyorder.SupplyOrderDO;
import cn.iocoder.yudao.module.wine.service.supplyorder.SupplyOrderService;

@Tag(name = "管理后台 - 供求订单管理")
@RestController
@RequestMapping("/wine/supply-order")
@Validated
public class SupplyOrderController {

    @Resource
    private SupplyOrderService supplyOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建供求订单管理")
    @PreAuthorize("@ss.hasPermission('wine:supply-order:create')")
    public CommonResult<Long> createSupplyOrder(@Valid @RequestBody SupplyOrderSaveReqVO createReqVO) {
        return success(supplyOrderService.createSupplyOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供求订单管理")
    @PreAuthorize("@ss.hasPermission('wine:supply-order:update')")
    public CommonResult<Boolean> updateSupplyOrder(@Valid @RequestBody SupplyOrderSaveReqVO updateReqVO) {
        supplyOrderService.updateSupplyOrder(updateReqVO);
        return success(true);
    }

    @PutMapping("/updateStatus")
    @Operation(summary = "更新订单状态，并记录日志 orderLog")
    @PreAuthorize("@ss.hasPermission('wine:supply-order:update')")
    public CommonResult<Boolean> updateSupplyOrderStatus(@Valid @RequestBody SupplyOrderSaveReqVO updateReqVO) {
        supplyOrderService.updateSupplyOrderStatus(updateReqVO);
        return success(true);
    }

    @PostMapping("/cancelOrder")
    @Operation(summary = "后台取消订单")
    @PreAuthorize("@ss.hasPermission('wine:supply-order:update')")
    public CommonResult<Boolean> updateSupplyOrderStatus(@RequestParam("id") Long id) {
        supplyOrderService.cancelOrder(id);
        return success(true);
    }
    @DeleteMapping("/delete")
    @Operation(summary = "删除供求订单管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wine:supply-order:delete')")
    public CommonResult<Boolean> deleteSupplyOrder(@RequestParam("id") Long id) {
        supplyOrderService.deleteSupplyOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供求订单管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wine:supply-order:query')")
    public CommonResult<SupplyOrderRespVO> getSupplyOrder(@RequestParam("id") Long id) {
        SupplyOrderRespVO supplyOrder = supplyOrderService.getSupplyOrderInfo(id);
        return success(supplyOrder);
    }

    @GetMapping("/page")
    @Operation(summary = "获得供求订单管理分页")
    @PreAuthorize("@ss.hasPermission('wine:supply-order:query')")
    public CommonResult<PageResult<SupplyOrderRespVO>> getSupplyOrderPage(@Valid SupplyOrderPageReqVO pageReqVO) {
        PageResult<SupplyOrderDO> pageResult = supplyOrderService.getSupplyOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SupplyOrderRespVO.class));
    }

    @GetMapping("/new-page")
    @Operation(summary = "获得供求订单管理分页")
    @PreAuthorize("@ss.hasPermission('wine:supply-order:query')")
    public CommonResult<PageResult<SupplyOrderRespVO>> getSupplyOrderNewPage(@Valid SupplyOrderPageReqVO pageReqVO) {
        PageResult<SupplyOrderRespVO> pageResult = supplyOrderService.getSupplyOrderNewPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供求订单管理 Excel")
    @PreAuthorize("@ss.hasPermission('wine:supply-order:export')")
    @OperateLog(type = EXPORT)
    public void exportSupplyOrderExcel(@Valid SupplyOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SupplyOrderDO> list = supplyOrderService.getSupplyOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "供求订单管理.xls", "数据", SupplyOrderRespVO.class,
                        BeanUtils.toBean(list, SupplyOrderRespVO.class));
    }

}
